package com.supply.dl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.supply.dl.R;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.BaseItemBean;
import com.supply.dl.bean.InAndOutBoundLocationBean;
import com.supply.dl.bean.MaterielPositionInfoBean;
import com.supply.dl.bean.OperateFlagEnum;
import com.supply.dl.bean.OrderBoundDetailBean;
import com.supply.dl.bean.PositionDetailBean;
import com.supply.dl.bean.PositionInfoBean;
import com.supply.dl.bean.RecommendPositionInfoBean;
import com.supply.dl.bean.SearchStockBean;
import com.supply.dl.bean.SelectDictionaryBean;
import com.supply.dl.bean.request.ConfirmLocationNoticeDetailRequest;
import com.supply.dl.constant.Constants;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.BigDecimalUtil;
import com.supply.dl.utils.CommonNormalAdapterUtil;
import com.supply.dl.utils.InterfaceUtil;
import com.supply.dl.utils.RecyclerViewUtil;
import com.supply.dl.utils.StringUtils;
import com.supply.dl.utils.Utils;
import com.supply.dl.utils.utils.ToastUtil;
import com.supply.dl.view.MaxRecyclerView;
import com.supply.dl.widget.LoadTipFailedLayout;
import com.supply.dl.widget.adapter.CommonNormalAdapter;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

/**
 * Created by ${qianzhihe} on 2018/4/27.
 * 入库定位详情页面（通知单定位详情页面）
 * 出库定位详情界面
 */

public class InAndOutBoundLocationDetailActivity extends BaseActivity{

    InAndOutBoundLocationBean boundLocationBean;
    int activityFlag;

    /**
     * head头部数据
     */
    @BindView(R.id.common_letter_of_notice_info_noticeLetterNoTv)
    TextView noticeLetterTv;
    @BindView(R.id.common_letter_of_notice_info_showNoticeLetterNo)
    TextView showNoticeLetterNo;
    @BindView(R.id.common_letter_of_notice_info_positionStatusName)
    TextView positionStatusName;
    @BindView(R.id.common_letter_of_notice_info_showFirstValue)
    TextView showFirstValue;
    @BindView(R.id.common_letter_of_notice_info_secondTv)
    TextView showSecondTv;
    @BindView(R.id.common_letter_of_notice_info_showSecondValue)
    TextView showSecondValue;
    @BindView(R.id.common_letter_of_notice_info_showThirdValue)
    TextView showThirdValue;
    @BindView(R.id.common_letter_of_notice_info_showFourthValue)
    TextView showFourthValue;
    @BindView(R.id.common_letter_of_notice_info_fivthTv)
    TextView showFivethTv;
    @BindView(R.id.common_letter_of_notice_info_showFivthValue)
    TextView showFivthValue;

    /**
     * 物料列表
     */
    @BindView(R.id.inbound_location_detail_main_materielRecyclerView)
    MaxRecyclerView materielRecyclerView;
    CommonNormalAdapter materielAdapter;
    int materielAdapterFlag;
    List<BaseItemBean> itemMaterielList;
    PositionDetailBean mPositionDetailBean;

    @BindView(R.id.inbound_location_detail_main_materielLineView)
    View materielLineView;
    @BindView(R.id.inbound_location_detail_main_commonMaterielPositionInfoLayout)
    RelativeLayout commonMaterielPositionInfoLayout;

    @BindView(R.id.common_materiel_position_info_showFirstValue)
    TextView showMaterielFirstValue;//物料编码
    @BindView(R.id.common_materiel_position_info_showSecondValue)
    TextView showMaterielSecondValue;//物料名称
    @BindView(R.id.common_materiel_position_info_showThirdValue)
    TextView showMaterielThirdValue;//总数
    @BindView(R.id.common_materiel_position_info_fourthTv)
    TextView materielFourthTv;
    @BindView(R.id.common_materiel_position_info_showFourthValue)
    TextView showMaterielFourthValue;//未定位数量、已定位数量

    /**
     * 加载框
     */
    @BindView(R.id.inbound_location_detail_main_loadView)
    LoadTipFailedLayout loadTipFailedLayout;

    @BindView(R.id.inbound_location_detail_main_notLocatedTv)
    TextView notLocatedTv;//未定位
    @BindView(R.id.inbound_location_detail_main_notLocatedLineView)
    View notLocatedLineView;

    ArrayList<MaterielPositionInfoBean> notPositionInfo;//未定位信息
    ArrayList<MaterielPositionInfoBean> hasPositionInfo;//已定位信息

    @BindView(R.id.inbound_location_detail_main_hadLocatedTv)
    TextView hadLocatedTv;//已定位
    @BindView(R.id.inbound_location_detail_main_hadLocatedLineView)
    View hadLocatedLineView;

    boolean isLocation=false;//是否定位

    @BindView(R.id.inbound_location_detail_main_addNewPositionBtn)
    TextView addNewPositionBtn;//添加新定位
    @BindView(R.id.inbound_location_detail_main_batchPositionBtn)
    TextView batchPositionBtn;//批量定位

    @BindView(R.id.inbound_location_detail_main_scroll)
    ScrollView scrollAll;
    /**
     * 定位列表
     */
    @BindView(R.id.inbound_location_detail_main_positionRecyclerView)
    MaxRecyclerView positionRecyclerView;
    CommonNormalAdapter positionAdapter;
    int positionAdapterFlag;
    List<BaseItemBean> itemPositionList;
    int currentSelectedMaterielPosition=0;//当前物料选中的item位置

    boolean isInBound;//是否是入库
    int currentSelectedWorkPosition=-1;

    int itemConunt=0;//存在未定位信息里面，没有推荐库位的情况

    @Override
    protected int getLayoutResource() {
        return R.layout.inbound_location_detail_main;
    }

    @Override
    protected void initView() {
        super.initView();
        showLeftBack();
    }

    @Override
    protected void initLoadingView() {
        super.initLoadingView();
        loadTipFailedLayout.setRestartOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshView();
            }
        });
    }

    private void refreshView() {
        if (activityFlag==Constants.FLAG_ACTIVITY_IN_BOUND_LOCATION_DETAIL_UI){
            isInBound=true;
        }else if (activityFlag==Constants.FLAG_ACTIVITY_OUT_BOUND_LOCATION_DETAIL_UI){
            isInBound=false;
        }
        getPositionDetail(isInBound);
    }

    @Override
    protected void initListener() {
        super.initListener();
        setRightHomeClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishAllActivityExceptAppointed(MainActivity.class);
            }
        });

        setRightScanClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.startScanActivity(InAndOutBoundLocationDetailActivity.this);
            }
        });
        notLocatedTv.setOnClickListener(this);
        hadLocatedTv.setOnClickListener(this);
        addNewPositionBtn.setOnClickListener(this);
        batchPositionBtn.setOnClickListener(this);
        InterfaceUtil.getInstance().setInterfaceCallBackListener(interfacesCallBackListener);
    }

    InterfaceUtil.InterfacesCallBackListener  interfacesCallBackListener=new InterfaceUtil.InterfacesCallBackListener() {
        @Override
        public void onSuccess(BaseInfo<?> baseInfo, Enum opertateFlagEnum) {
            if (opertateFlagEnum==OperateFlagEnum.NOTICE_LOCATION_DETAIL_FOR_CONFIRM_LOCATION){//确认定位
                ToastUtil.showToast(InAndOutBoundLocationDetailActivity.this, "确认定位成功");
            }else if(opertateFlagEnum==OperateFlagEnum.NOTICE_LOCATION_DETAIL_FOR_DELETE_LOCATION){//删除

            }
            if (!isLocation) {
                // 当前是未定位页面时，才执行刷新页面的操作
                refreshView();
            }
        }

        @Override
        public void onFailure(String message, Enum opertateFlagEnum) {
            if (opertateFlagEnum==OperateFlagEnum.NOTICE_LOCATION_DETAIL_FOR_CONFIRM_LOCATION){

            }else if(opertateFlagEnum==OperateFlagEnum.NOTICE_LOCATION_DETAIL_FOR_DELETE_LOCATION){

            }
            ToastUtil.showToast(InAndOutBoundLocationDetailActivity.this, message);
        }

        @Override
        public void onSuccess(BaseInfo<?> baseInfo) {

        }

        @Override
        public void onFailure(String message) {
        }
    };

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        boundLocationBean= (InAndOutBoundLocationBean) getIntent().getSerializableExtra("InAndOutBoundLocationBean");
        activityFlag=getIntent().getIntExtra(Constants.FLAG_ACTIVITY_KEY,  Constants.FLAG_ACTIVITY_IN_BOUND_LOCATION_DETAIL_UI);
        itemMaterielList=new ArrayList<>();
        itemPositionList=new ArrayList<>();
        notPositionInfo=new ArrayList<>();
        hasPositionInfo=new ArrayList<>();
        showFivethTv.setVisibility(View.VISIBLE);
        showFivthValue.setVisibility(View.VISIBLE);
        if (!StringUtils.isEmpty(boundLocationBean.noticeOrderId)){
            setTitleShow(getResourcesString(R.string.letterOfNoticeLocationDetail));
        }else {
            setTitleShow(getResourcesString(R.string.letterOfOrderLocationDetail));
        }
        //入库定位、出入定位的通知单定位详情使用同一个标识
        materielAdapterFlag=CommonNormalAdapter.FLAG_IN_BOUND_LOCATION_DETAIL_MATREIEL_MAIN;
        if (activityFlag==Constants.FLAG_ACTIVITY_IN_BOUND_LOCATION_DETAIL_UI){
            positionAdapterFlag=CommonNormalAdapter.FLAG_IN_BOUND_LOCATION_DETAIL_POSITION_MAIN;
            isInBound=true;
        }else if (activityFlag==Constants.FLAG_ACTIVITY_OUT_BOUND_LOCATION_DETAIL_UI){
            positionAdapterFlag=CommonNormalAdapter.FLAG_IN_BOUND_LOCATION_DETAIL_POSITION_MAIN_OUT;
            isInBound=false;
        }
        if (isInBound && StringUtils.isEmpty(boundLocationBean.noticeOrderId)) {//入库
            noticeLetterTv.setText("入库订单号");
        } else if (!isInBound && StringUtils.isEmpty(boundLocationBean.noticeOrderId)){
            noticeLetterTv.setText("出库订单号");
        }
        getPositionDetail(isInBound);
        isHadLocation(isLocation);
    }

    @Override
    protected void initRecyclerView() {
        super.initRecyclerView();
        RecyclerViewUtil.initLinearLayoutHorizontal(this, materielRecyclerView);
        materielAdapter= CommonNormalAdapterUtil.initCommonNormalAdapter(R.layout.materiel_position_info_item, itemMaterielList, materielAdapterFlag);
        /**
         * 物料item点击事件
         */
        materielAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                currentSelectedMaterielPosition=position;
                showNoOrHadLocationInfo(position);
            }
        });
        materielRecyclerView.setAdapter(materielAdapter);

        RecyclerViewUtil.initLinearLayoutVERTICAL(this, positionRecyclerView);
        positionAdapter= CommonNormalAdapterUtil.initCommonNormalAdapter(R.layout.position_info_item, itemPositionList, positionAdapterFlag);
        /**
         * 定位列表item点击事件
         * 删除点击、确认定位点击
         */
        positionAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                currentSelectedWorkPosition=position;
                singleLocationItemClick(position, view);
            }
        });
        positionRecyclerView.setAdapter(positionAdapter);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.inbound_location_detail_main_notLocatedTv://未定位
                isHadLocation(false);
                dealMaterielData(notPositionInfo);
                currentSelectedMaterielPosition=0;
                showNoOrHadLocationInfo(currentSelectedMaterielPosition);//默认选中未定位的第一个
                break;
            case R.id.inbound_location_detail_main_hadLocatedTv://已定位
                isHadLocation(true);
                dealMaterielData(hasPositionInfo);
                currentSelectedMaterielPosition=0;
                showNoOrHadLocationInfo(currentSelectedMaterielPosition);//默认选中已定位的第一个
                break;
            case R.id.inbound_location_detail_main_addNewPositionBtn://添加新定位(动态添加itemView)
                /**
                 * itemConunt：当前添加的itemView后的iten总数
                 */
                itemConunt++;
                addLocationItem(itemConunt);
                break;
            case R.id.inbound_location_detail_main_batchPositionBtn://批量定位
                Bundle bundle=new Bundle();
                bundle.putSerializable("InAndOutBoundLocationBean", boundLocationBean);
                //操作类型(0:入库,1:出库)
                if (activityFlag==Constants.FLAG_ACTIVITY_IN_BOUND_LOCATION_DETAIL_UI){
                    bundle.putInt("operateType", 0);
                }else if (activityFlag==Constants.FLAG_ACTIVITY_OUT_BOUND_LOCATION_DETAIL_UI){
                    bundle.putInt("operateType", 1);
                }
                if (itemMaterielList.size()>0){
                    bundle.putSerializable("MaterielPositionInfoBean", (MaterielPositionInfoBean) itemMaterielList.get(currentSelectedMaterielPosition).object);
                }
                jumpActivity(BatchPositioningActivity.class, bundle);
                break;
        }
    }

    /**
     * 是否定位
     * @param isTempLocation
     */
    private void isHadLocation(boolean isTempLocation){
        isLocation=isTempLocation;
        if (!isTempLocation){
            addNewPositionBtn.setBackgroundColor(getResources().getColor(R.color.color_18C299));
            batchPositionBtn.setVisibility(View.VISIBLE);
            notLocatedLineView.setVisibility(View.VISIBLE);
            hadLocatedLineView.setVisibility(View.INVISIBLE);
            notLocatedTv.setTextColor(getResources().getColor(R.color.blue));
            hadLocatedTv.setTextColor(getResources().getColor(R.color.color_gray));
        }else {
            hadLocatedLineView.setVisibility(View.VISIBLE);
            notLocatedLineView.setVisibility(View.INVISIBLE);
            batchPositionBtn.setVisibility(View.GONE);
            hadLocatedTv.setTextColor(getResources().getColor(R.color.blue));
            notLocatedTv.setTextColor(getResources().getColor(R.color.color_gray));
            addNewPositionBtn.setBackgroundColor(getResources().getColor(R.color.blue));
        }
    }

    /**
     * 获取通知单定位详情
     */
    private void getPositionDetail(final boolean tempIsInBound){
        loadTipFailedLayout.setLoadText(R.string.since_help_loading);
        loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_PROGRESS);
        HttpManger httpManger=HttpManger.getInstance();
        httpManger.setContext(this);
        if (tempIsInBound){
            httpManger.getPositionInBoundDetail(boundLocationBean.orderId, boundLocationBean.noticeOrderId);
        }else {
            httpManger.getPositionOutBoundDetail(boundLocationBean.orderId, boundLocationBean.noticeOrderId);
        }
        httpManger.setHttpResponseCallBackListener(this);
    }

    @Override
    public void onSuccess(BaseInfo<?> baseInfo) {
        super.onSuccess(baseInfo);
            mPositionDetailBean= (PositionDetailBean) baseInfo.getData();
            if (mPositionDetailBean!=null){
                loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FINISH);

                if (!StringUtils.isEmpty(boundLocationBean.noticeOrderId)) {
                    showNoticeLetterNo.setText(mPositionDetailBean.noticeOrderNo);//通知单号
                } else {
                    showNoticeLetterNo.setText(mPositionDetailBean.orderNo);//订单号
                }
                positionStatusName.setText(mPositionDetailBean.positionStatusName);//定库状态
                showFirstValue.setText(mPositionDetailBean.bussTypeName);//业务类型
                showSecondValue.setText(mPositionDetailBean.supplierName);//供应商
                showThirdValue.setText(mPositionDetailBean.carNo);//车牌号
                showFourthValue.setText(mPositionDetailBean.driverName);//司机
                showFivthValue.setText(mPositionDetailBean.remark);//备注
                if (isInBound){//入库定位详情
                }else {//出库定位详情
                    showSecondTv.setText(getResourcesString(R.string.supplierOut));//经销商
                }

                if (mPositionDetailBean.getNotPositionInfo()!=null){
                    notPositionInfo.clear();
                    notPositionInfo.addAll(mPositionDetailBean.getNotPositionInfo());
                    if (notPositionInfo.size()>0){
                        for (int i=0; i<notPositionInfo.size(); i++){
                            if (i==0){
                                notPositionInfo.get(i).isSelected=true;
                            }else {
                                notPositionInfo.get(i).isSelected=false;
                            }
                        }
                    }
                }
                if (mPositionDetailBean.getHasPositionInfo()!=null){
                    hasPositionInfo.clear();
                    hasPositionInfo.addAll(mPositionDetailBean.getHasPositionInfo());
                    if (hasPositionInfo.size()>0){
                        for (int i=0; i<hasPositionInfo.size(); i++){
                            if (i==0){
                                hasPositionInfo.get(i).isSelected=true;
                            }else {
                                hasPositionInfo.get(i).isSelected=false;
                            }
                        }
                    }
                }
                dealMaterielData(notPositionInfo);
                showNoOrHadLocationInfo(0);
            }else {
                loadTipFailedLayout.setTextColor(R.color.color_gray);
                loadTipFailedLayout.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
                loadTipFailedLayout.setNormalText(R.string.noMessage);
                loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_NORMAL);
            }

    }

    @Override
    public void onFailure(String message) {
        super.onFailure(message);
        loadTipFailedLayout.setTextColor(R.color.color_gray);
        loadTipFailedLayout.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        loadTipFailedLayout.setNormalText(R.string.since_help_load_failed);
        loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FAILED);
        ToastUtil.showToast(InAndOutBoundLocationDetailActivity.this, message);
    }

    /**
     * 处理未定位、已定位列表数据（物料）
     * @param materielPositionInfoBeanList
     */
    private void dealMaterielData(ArrayList<MaterielPositionInfoBean> materielPositionInfoBeanList){
        itemMaterielList.clear();
        if (materielPositionInfoBeanList.size()>0){
            for (MaterielPositionInfoBean materielPositionInfoBean: materielPositionInfoBeanList){
                itemMaterielList.add(new BaseItemBean(materielPositionInfoBean));
            }
            setLocationUIIsShow(true);
        }else {
            setLocationUIIsShow(false);
        }
        materielAdapter.setNewData(itemMaterielList);
    }

    /**
     * 处理未定位物料item点击后，对应的定位数据
     * @param positionInfoList
     */
    private void dealNotPositionData(ArrayList<RecommendPositionInfoBean> positionInfoList){
        itemPositionList.clear();
        if (positionInfoList.size()>0){
            for (RecommendPositionInfoBean recommendPositionInfoBean: positionInfoList){
                itemPositionList.add(new BaseItemBean(recommendPositionInfoBean));
            }
            positionRecyclerView.setVisibility(View.VISIBLE);
        }else {
            // 对未定位，有物料，无默认定位信息时，位置信息也应该是可见。
            // 如果打开此处代码，有物料，无默认定位信息时，点击添加定位时，则看不到新增
            //positionRecyclerView.setVisibility(View.INVISIBLE);

            RecommendPositionInfoBean recommendPositionInfoBean=new RecommendPositionInfoBean();
            recommendPositionInfoBean.amount= BigDecimalUtil.getBigDecimalByString("0");
            recommendPositionInfoBean.weight= BigDecimalUtil.getBigDecimalByString("0");
            recommendPositionInfoBean.volume= BigDecimalUtil.getBigDecimalByString("0");
            //itemPositionList.add(position, new BaseItemBean(recommendPositionInfoBean));
            itemPositionList.add(new BaseItemBean(recommendPositionInfoBean));
        }
        positionAdapter.setNewData(itemPositionList);
        itemConunt=itemPositionList.size();
    }

    /**
     * 处理已定位物料item点击后，对应的定位数据
     * @param positionInfoList
     */
    private void dealHasPositionData(ArrayList<PositionInfoBean> positionInfoList){
        itemPositionList.clear();
        if (positionInfoList.size()>0){
            for (PositionInfoBean positionInfoBean: positionInfoList){
                itemPositionList.add(new BaseItemBean(positionInfoBean));
            }
            positionRecyclerView.setVisibility(View.VISIBLE);
        }else {
            positionRecyclerView.setVisibility(View.INVISIBLE);
        }
        positionAdapter.setNewData(itemPositionList);
        itemConunt=itemPositionList.size();
    }

    /**
     * 展示当前未定位或者已定位信息
     * @param currentPosition
     */
    private void showNoOrHadLocationInfo(int currentPosition){
        if (itemMaterielList.size()<=0){
            return;
        }
        MaterielPositionInfoBean materielPositionInfoBean= (MaterielPositionInfoBean) itemMaterielList.get(currentPosition).object;
        if (materielPositionInfoBean!=null){
            showMaterielFirstValue.setText(materielPositionInfoBean.skuCode);
            showMaterielSecondValue.setText(materielPositionInfoBean.skuName);
            showMaterielThirdValue.setText(materielPositionInfoBean.detailAmount+"件/"+materielPositionInfoBean.detailWeight+"吨");
        }

        for (BaseItemBean baseItemBean : itemMaterielList){
            ((MaterielPositionInfoBean)baseItemBean.object).isSelected=false;
        }

        ((MaterielPositionInfoBean) itemMaterielList.get(currentPosition).object).isSelected=true;
        materielAdapter.setNewData(itemMaterielList);
        if (!isLocation){
            materielFourthTv.setText(getResourcesString(R.string.notPositionNumber));
            if (materielPositionInfoBean!=null){
                showMaterielFourthValue.setText(materielPositionInfoBean.notPositionAmount+"件/"
                        + materielPositionInfoBean.notPositionWeight+"吨");
            }
            if (materielPositionInfoBean!=null && materielPositionInfoBean.getRecommendPositionInfoList()!=null){
                dealNotPositionData(materielPositionInfoBean.getRecommendPositionInfoList());
            }else {
                positionRecyclerView.setVisibility(View.INVISIBLE);
            }
        }else {
            materielFourthTv.setText(getResourcesString(R.string.hadPositionNumber));
            if (materielPositionInfoBean!=null){
                showMaterielFourthValue.setText(materielPositionInfoBean.detailAmount.subtract(materielPositionInfoBean.notPositionAmount)+"件/"
                        + materielPositionInfoBean.detailWeight.subtract(materielPositionInfoBean.notPositionWeight)+"吨");
            }
            if (materielPositionInfoBean!=null && materielPositionInfoBean.getPositionInfoList()!=null){
                dealHasPositionData(materielPositionInfoBean.getPositionInfoList());
            }else {
                positionRecyclerView.setVisibility(View.INVISIBLE);
            }
        }

    }

    /**
     * 设置未定位、已定位模块是否展示
     * @param isHasData
     */
    private void setLocationUIIsShow(boolean isHasData){
        if (isHasData){
            materielRecyclerView.setVisibility(View.VISIBLE);
            materielLineView.setVisibility(View.VISIBLE);
            commonMaterielPositionInfoLayout.setVisibility(View.VISIBLE);
            positionRecyclerView.setVisibility(View.VISIBLE);
        }else {
            materielRecyclerView.setVisibility(View.GONE);
            materielLineView.setVisibility(View.GONE);
            commonMaterielPositionInfoLayout.setVisibility(View.GONE);
            positionRecyclerView.setVisibility(View.GONE);
        }
    }

    /**
     * 单个物料item下定位列表的item上点击事件
     * 未定位模块（单个确认定位操作）
     * @param currentSelectedLocationPosition 当前定位列表点击item的position
     * @param view
     */
    private void singleLocationItemClick(int currentSelectedLocationPosition, View view){
        EditText edtShowNumberForPiece = (EditText) positionAdapter.getViewByPosition(positionRecyclerView, currentSelectedLocationPosition, R.id.position_info_item_showNumberForPieceTv);
        EditText edtShowWeightForTon = (EditText) positionAdapter.getViewByPosition(positionRecyclerView, currentSelectedLocationPosition, R.id.position_info_item_showWeightForTonTv);
        EditText edtShowVolumeForSquare = (EditText) positionAdapter.getViewByPosition(positionRecyclerView, currentSelectedLocationPosition, R.id.position_info_item_showVolumeForSquareTv);

        //当前选中的物料item对象
        MaterielPositionInfoBean currentMaterielPositionInfoBean= (MaterielPositionInfoBean) itemMaterielList.get(currentSelectedMaterielPosition).object;
        //当前选中的物料item列表下点击定位列表的item
        BaseItemBean baseItemBean=itemPositionList.get(currentSelectedLocationPosition);
        ConfirmLocationNoticeDetailRequest noticeDetailRequest=new ConfirmLocationNoticeDetailRequest();
        noticeDetailRequest.orderId=boundLocationBean.orderId;
        noticeDetailRequest.orderNo=boundLocationBean.orderNo;
        noticeDetailRequest.noticeOrderId=boundLocationBean.noticeOrderId;
        noticeDetailRequest.noticeOrderNo=boundLocationBean.noticeOrderNo;
        noticeDetailRequest.skuId=currentMaterielPositionInfoBean.skuId;
        noticeDetailRequest.skuCode=currentMaterielPositionInfoBean.skuCode;
        noticeDetailRequest.skuName=currentMaterielPositionInfoBean.skuName;
        noticeDetailRequest.orderDetailId=currentMaterielPositionInfoBean.orderDetailId;
        String ids="";
        if (!isLocation){
            RecommendPositionInfoBean recommendPositionInfoBean= (RecommendPositionInfoBean) baseItemBean.object;
            noticeDetailRequest.noticeOrderDetailId=recommendPositionInfoBean.noticeOrderDetailId;
            noticeDetailRequest.whId=getStringByIsEmpty(recommendPositionInfoBean.recommendWhId);
            noticeDetailRequest.stockId=recommendPositionInfoBean.recommendStockId;
            noticeDetailRequest.invId=recommendPositionInfoBean.invId;
            noticeDetailRequest.recommendStockId=recommendPositionInfoBean.recommendStockId;
            noticeDetailRequest.recommendWhId=recommendPositionInfoBean.recommendWhId;
            noticeDetailRequest.amount=recommendPositionInfoBean.amount;
            noticeDetailRequest.weight=recommendPositionInfoBean.weight;
            noticeDetailRequest.volume=recommendPositionInfoBean.volume;
            ids=recommendPositionInfoBean.id;//定位ID
        }else {
            PositionInfoBean positionInfoBean= (PositionInfoBean) baseItemBean.object;
            noticeDetailRequest.noticeOrderDetailId=positionInfoBean.noticeOrderDetailId;
            noticeDetailRequest.whId=getStringByIsEmpty(positionInfoBean.whId);
            noticeDetailRequest.stockId=positionInfoBean.stockId;
            noticeDetailRequest.invId=positionInfoBean.invId;
            noticeDetailRequest.recommendStockId=positionInfoBean.recommendStockId;
            noticeDetailRequest.recommendWhId=positionInfoBean.recommendWhId;
            noticeDetailRequest.amount=positionInfoBean.amount;
            noticeDetailRequest.weight=positionInfoBean.weight;
            noticeDetailRequest.volume=positionInfoBean.volume;
            ids=positionInfoBean.id;//定位ID
        }
        //操作类型(0:入库,1:出库)
        if (activityFlag==Constants.FLAG_ACTIVITY_IN_BOUND_LOCATION_DETAIL_UI){//入库定位
            noticeDetailRequest.operateType=0;
        }else {//出库定位
            noticeDetailRequest.operateType=1;
        }

        if (!isLocation){
            RecommendPositionInfoBean recommendPositionInfoBean= (RecommendPositionInfoBean) baseItemBean.object;
            recommendPositionInfoBean.amount=BigDecimalUtil.getBigDecimalByString(edtShowNumberForPiece.getText().toString());
            recommendPositionInfoBean.weight=BigDecimalUtil.getBigDecimalByString(edtShowWeightForTon.getText().toString());
            recommendPositionInfoBean.volume=BigDecimalUtil.getBigDecimalByString(edtShowVolumeForSquare.getText().toString());
        }else {
            PositionInfoBean positionInfoBean= (PositionInfoBean) baseItemBean.object;
            positionInfoBean.amount=BigDecimalUtil.getBigDecimalByString(edtShowNumberForPiece.getText().toString());
            positionInfoBean.weight=BigDecimalUtil.getBigDecimalByString(edtShowWeightForTon.getText().toString());
            positionInfoBean.volume=BigDecimalUtil.getBigDecimalByString(edtShowVolumeForSquare.getText().toString());
        }

        Intent intent=null;
        int reqCode=-1;
        switch (view.getId()){
            case R.id.position_info_item_surePositionBtn://确定定位
//                if (StringUtils.isEmpty(ids)){
                    if (!isLocation){
                        RecommendPositionInfoBean recommendPositionInfoBean= (RecommendPositionInfoBean) baseItemBean.object;
                        recommendPositionInfoBean.amount=BigDecimalUtil.getBigDecimalByString(edtShowNumberForPiece.getText().toString());
                        recommendPositionInfoBean.weight=BigDecimalUtil.getBigDecimalByString(edtShowWeightForTon.getText().toString());
                        recommendPositionInfoBean.volume=BigDecimalUtil.getBigDecimalByString(edtShowVolumeForSquare.getText().toString());
                    }else {
                        PositionInfoBean positionInfoBean= (PositionInfoBean) baseItemBean.object;
                        positionInfoBean.amount=BigDecimalUtil.getBigDecimalByString(edtShowNumberForPiece.getText().toString());
                        positionInfoBean.weight=BigDecimalUtil.getBigDecimalByString(edtShowWeightForTon.getText().toString());
                        positionInfoBean.volume=BigDecimalUtil.getBigDecimalByString(edtShowVolumeForSquare.getText().toString());
                    }
                    if (StringUtils.isEmpty(noticeDetailRequest.stockId)){
                        ToastUtil.showToast(InAndOutBoundLocationDetailActivity.this, getResourcesString(R.string.toastPleaseSeletedStockPosition));
                        return;
                    }
                    if (StringUtils.isEmpty(noticeDetailRequest.whId)){
                        ToastUtil.showToast(InAndOutBoundLocationDetailActivity.this, getResourcesString(R.string.toastPleaseSeletedWareHouse));
                        return;
                    }

                    if (checkNumberIsEmpty(edtShowNumberForPiece, getResourcesString(R.string.toastNumberNotEmpty))){
                        return;
                    }
                    if (checkNumberIsEmpty(edtShowWeightForTon, getResourcesString(R.string.toastWeightNotEmpty))){
                        return;
                    }
                    if (checkNumberIsEmpty(edtShowVolumeForSquare, getResourcesString(R.string.toastVolumeNotEmpty))){
                        return;
                    }

                    if (checkNumberIsMoreThanZero(edtShowNumberForPiece, getResourcesString(R.string.toastNumberMustMoreThanZero))){
                        return;
                    }
                    if (checkNumberIsMoreThanZero(edtShowWeightForTon, getResourcesString(R.string.toastWeightMustMoreThanZero))){
                        return;
                    }
                    if (checkNumberIsMoreThanZero(edtShowVolumeForSquare, getResourcesString(R.string.toastVolumeMustMoreThanZero))){
                        return;
                    }
//                }
                //获取当前提交时的数量、重量、体积值
                noticeDetailRequest.amount=BigDecimalUtil.getBigDecimalByString(edtShowNumberForPiece.getText().toString());
                noticeDetailRequest.weight=BigDecimalUtil.getBigDecimalByString(edtShowWeightForTon.getText().toString());
                noticeDetailRequest.volume=BigDecimalUtil.getBigDecimalByString(edtShowVolumeForSquare.getText().toString());

                InterfaceUtil.getInstance().confirmLocationByNoticeLocationDetail(noticeDetailRequest, OperateFlagEnum.NOTICE_LOCATION_DETAIL_FOR_CONFIRM_LOCATION,
                        InAndOutBoundLocationDetailActivity.this, isInBound);
                break;
            case R.id.position_info_item_positionDeleteTv://删除该定位
                if (StringUtils.isEmpty(ids)){//此时该item无数据，为新增的item，故直接将列表item删除即可，无需调用接口
                    itemConunt--;
                    itemPositionList.remove(currentSelectedLocationPosition);
                    positionAdapter.setNewData(itemPositionList);
                    return;
                }
                if (isInBound){
                    InterfaceUtil.getInstance().deleteLocationByNoticeLocationDetail(ids, OperateFlagEnum.NOTICE_LOCATION_DETAIL_FOR_DELETE_LOCATION,
                            InAndOutBoundLocationDetailActivity.this);
                }else {
                    InterfaceUtil.getInstance().deleteLocationByNoticeLocationDetailOutBound(ids, OperateFlagEnum.NOTICE_LOCATION_DETAIL_FOR_DELETE_LOCATION,
                            InAndOutBoundLocationDetailActivity.this);
                }
                break;
            case R.id.position_info_item_showReservoirPositionTv://库位选择
                intent= new Intent(this, DepotSelectActivity.class);
                if (activityFlag==Constants.FLAG_ACTIVITY_IN_BOUND_LOCATION_DETAIL_UI){
                    reqCode = AppConfig.BaseDataAction.REQCODE_NOTICE_IN_BOUND_DETAIL_DETAIL_BY_DEPOT;
                }else {
                    reqCode = AppConfig.BaseDataAction.REQCODE_NOTICE_OUT_BOUND_DETAIL_DETAIL_BY_DEPOT;
                }
                if (isInBound) {// 入库定位
                    intent.putExtra("showType", DepotSelectActivity.TYPE_IN_STOCK);
                } else {//出库定位
                    intent.putExtra("showType", DepotSelectActivity.TYPE_OUT_STOCK);
                }
                startActivityForResult(intent, reqCode);
                break;
            case R.id.position_info_item_showStorehouseTv://仓库选择
                String action="";
                intent= new Intent(this, SelectBaseDataActivity.class);
                //0：入库；1：出库
                if (activityFlag==Constants.FLAG_ACTIVITY_IN_BOUND_LOCATION_DETAIL_UI){
                    action = AppConfig.BaseDataAction.WARE_HOUSE_TYPE_0;
                    reqCode = AppConfig.BaseDataAction.REQCODE_NOTICE_IN_BOUND_DETAIL_DETAIL;
                }else {
                    action = AppConfig.BaseDataAction.WARE_HOUSE_TYPE_1;
                    reqCode = AppConfig.BaseDataAction.REQCODE_NOTICE_OUT_BOUND_DETAIL_DETAIL;
                }
                intent.putExtra("action", action);
                startActivityForResult(intent, reqCode);
                break;
        }
    }

    /**
     * 校验数量、重量、体积是否为空
     * @param editText
     * @param message
     */
    private boolean checkNumberIsEmpty(EditText editText, String message){
        if (StringUtils.isEmpty(editText.getText().toString())){
            ToastUtil.showToast(InAndOutBoundLocationDetailActivity.this, message);
            return true;
        }
        return false;
    }

    /**
     * 校验数量、重量、体积是否大于0
     * @param editText
     * @param message
     */
    private boolean checkNumberIsMoreThanZero(EditText editText, String message){
        if (Float.parseFloat(editText.getText().toString())<=0){
            ToastUtil.showToast(InAndOutBoundLocationDetailActivity.this, message);
            return true;
        }
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null || data.getSerializableExtra("selectBean") == null)
            return;
        BaseItemBean baseItemBean=itemPositionList.get(currentSelectedWorkPosition);
        switch (requestCode){
            case AppConfig.BaseDataAction.REQCODE_NOTICE_IN_BOUND_DETAIL_DETAIL://仓库
            case AppConfig.BaseDataAction.REQCODE_NOTICE_OUT_BOUND_DETAIL_DETAIL:
                SelectDictionaryBean selectBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
                if (!isLocation){
                    RecommendPositionInfoBean recommendPositionInfoBean= (RecommendPositionInfoBean) baseItemBean.object;
                    recommendPositionInfoBean.recommendWhName=selectBean.getText();
                    recommendPositionInfoBean.recommendWhId=selectBean.getValue();
                    baseItemBean.object=recommendPositionInfoBean;
                }else {
                    PositionInfoBean positionInfoBean= (PositionInfoBean) baseItemBean.object;
                    positionInfoBean.whName=selectBean.getText();
                    positionInfoBean.whId=selectBean.getValue();
                    baseItemBean.object=positionInfoBean;
                }
                positionAdapter.notifyItemChanged(currentSelectedWorkPosition);
                break;
            case AppConfig.BaseDataAction.REQCODE_NOTICE_IN_BOUND_DETAIL_DETAIL_BY_DEPOT://库位
            case AppConfig.BaseDataAction.REQCODE_NOTICE_OUT_BOUND_DETAIL_DETAIL_BY_DEPOT:
                SearchStockBean searchStockBean= (SearchStockBean) data.getSerializableExtra("selectBean");
                if (!isLocation){
                    RecommendPositionInfoBean recommendPositionInfoBean= (RecommendPositionInfoBean) baseItemBean.object;
                    recommendPositionInfoBean.recommendStockName=searchStockBean.getStockName();
                    recommendPositionInfoBean.recommendStockId=searchStockBean.getStockId();
                    recommendPositionInfoBean.recommendWhName=searchStockBean.getWhName();
                    recommendPositionInfoBean.recommendWhId=searchStockBean.getWhId();

                    recommendPositionInfoBean.stockStatusName=searchStockBean.getStockStatusName();
                    recommendPositionInfoBean.stockTypeName=searchStockBean.getStockTypeName();
                    recommendPositionInfoBean.unitName=searchStockBean.getUnitName();
                    recommendPositionInfoBean.batchNo=searchStockBean.getBatchNo();
                    baseItemBean.object=recommendPositionInfoBean;
                }else {
                    PositionInfoBean positionInfoBean= (PositionInfoBean) baseItemBean.object;
                    positionInfoBean.stockName=searchStockBean.getStockName();
                    positionInfoBean.stockId=searchStockBean.getStockId();
                    positionInfoBean.whName=searchStockBean.getWhName();
                    positionInfoBean.whId=searchStockBean.getWhId();

                    positionInfoBean.stockStatusName=searchStockBean.getStockStatusName();
                    positionInfoBean.stockTypeName=searchStockBean.getStockTypeName();
                    positionInfoBean.unitName=searchStockBean.getUnitName();
                    positionInfoBean.batchNo=searchStockBean.getBatchNo();
                    baseItemBean.object=positionInfoBean;
                }
                positionAdapter.notifyItemChanged(currentSelectedWorkPosition);
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 添加新定位数据
     * @param itemConunt 当前列表item的总数
     */
    public void addLocationItem(final int itemConunt) {
        /**
         * 设置当前添加itemView的位置，从0开始计算，故position=itemConunt-1
         */
        //final int position=itemConunt-1;
        if (!isLocation){
            RecommendPositionInfoBean recommendPositionInfoBean=new RecommendPositionInfoBean();
            recommendPositionInfoBean.amount= BigDecimalUtil.getBigDecimalByString("0");
            recommendPositionInfoBean.weight= BigDecimalUtil.getBigDecimalByString("0");
            recommendPositionInfoBean.volume= BigDecimalUtil.getBigDecimalByString("0");
            //itemPositionList.add(position, new BaseItemBean(recommendPositionInfoBean));
            itemPositionList.add(new BaseItemBean(recommendPositionInfoBean));
        }else {
            PositionInfoBean positionInfoBean=new PositionInfoBean();
            positionInfoBean.amount= BigDecimalUtil.getBigDecimalByString("0");
            positionInfoBean.weight= BigDecimalUtil.getBigDecimalByString("0");
            positionInfoBean.volume= BigDecimalUtil.getBigDecimalByString("0");
            //itemPositionList.add(position, new BaseItemBean(positionInfoBean));
            itemPositionList.add(new BaseItemBean(positionInfoBean));
        }
        //positionRecyclerView.smoothScrollToPosition(position); //此方法在右ScrollView的时候不起作用了
        positionAdapter.setNewData(itemPositionList);
        scrollToBottom();
    }

    /**
     * 移动到列表底部
     */
    private void scrollToBottom() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                scrollAll.fullScroll(ScrollView.FOCUS_DOWN);
            }
        }, 200);
    }

    private View getFooterView(int type, View.OnClickListener listener) {
        View view = getLayoutInflater().inflate(R.layout.position_info_item, (ViewGroup) positionRecyclerView.getParent(), false);
        view.findViewById(R.id.position_info_item_positionDeleteTv).setOnClickListener(listener);
        view.findViewById(R.id.position_info_item_surePositionBtn).setOnClickListener(listener);
        view.setOnClickListener(listener);
        return view;
    }

}
